//
//  YPHomePageVC.m
//  WHISPER
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 王艳苹. All rights reserved.
//

#import "YPHomePageVC.h"
#import "UIView+Extention.h"
#import "ButtonWithview.h"
#import "YPHomePageDynamicVC.h"
#import "YPHomePageFragmentVC.h"
#import "YPHomePageSellectWorldCell.h"
#import "YPNetWorking.h"
#import "YPMakeURL.h"
#import "YPSelectListModel.h"
#import "YPUser.h"
#import "YPHomePagePhotoCell.h"
#import "YPHomePageFragmentCell.h"
#import "YPHomePageMusicCell.h"
#import "YPHomePagePoemCell.h"
#import "YPHomePageSoundCell.h"
#import "YPScrollView.h"
#import "YPSelectWheelModel.h"
#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"
#import "YPWebViewVC.h"
#import "MJRefresh.h"
#import "YPSongController.h"
#import "RadioDetailListModel.h"
#import "YPLikeHelper.h"
#import "YPDataBase.h"
#import "YPJudgeNet.h"

@interface YPHomePageVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>

@property (nonatomic,strong) UILabel *littleLable;
@property (nonatomic,strong) UITableView *selectVC;
@property (nonatomic,strong) NSMutableArray *wheelArray;
@property (nonatomic,strong) NSMutableArray *listDataArr;
@property (nonatomic,assign) CGFloat imageH;
@property (nonatomic,assign) CGFloat worldH;
@property (nonatomic,strong) YPLikeHelper *like;

@property (nonatomic,strong) YPDataBase *dataase;

@end
static NSInteger flag = 0;
@implementation YPHomePageVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.selectVC = [[UITableView alloc]initWithFrame:CGRectMake(0,44, self.view.width, self.view.hight - 44) style:UITableViewStylePlain];
    self.selectVC.delegate = self;
    self.selectVC.dataSource = self;
    self.selectVC.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upFreashAction:)];
    self.selectVC.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downFreash)];
    [self.view addSubview:_selectVC];
    //晚安故事系列
    [self.selectVC registerNib:[UINib nibWithNibName:@"YPHomePageSellectWorldCell" bundle:nil] forCellReuseIdentifier:@"YPHomePageSellectWorldCell"];
    [self.selectVC registerNib:[UINib nibWithNibName:@"YPHomePageMusicCell" bundle:nil] forCellReuseIdentifier:@"YPHomePageMusicCell"];
    [self.selectVC registerNib:[UINib nibWithNibName:@"YPHomePagePoemCell" bundle:nil] forCellReuseIdentifier:@"YPHomePagePoemCell"];
    [self.selectVC registerNib:[UINib nibWithNibName:@"YPHomePageSoundCell" bundle:nil] forCellReuseIdentifier:@"YPHomePageSoundCell"];
    [self.selectVC registerNib:[UINib nibWithNibName:@"YPHomePagePhotoCell" bundle:nil] forCellReuseIdentifier:@"YPHomePagePhotoCell"];
    [self.selectVC registerNib:[UINib nibWithNibName:@"YPHomePageFragmentCell" bundle:nil] forCellReuseIdentifier:@"YPHomePageFragmentCell"];
    [self setButtonView];
    [self judgeNet];
}

- (void)judgeNet{
    if ([[YPJudgeNet shareJudgeNet] isConnection]) {
        [self getData];
    }else {
        [self.dataase getSelectDataFromDataBaseSuccess:^(NSArray *arr) {
            _listDataArr = (NSMutableArray*)arr;
            [self.selectVC reloadData];
        } wheel:^{
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0,110,self.view.width, 140)];
            lable.text = @"无网络连接";
            lable.textAlignment = NSTextAlignmentCenter;
            _selectVC.tableHeaderView = lable;
        } fail:^(NSError *err) {
            NSLog(@"%@",err);
        }];
    }
}
- (void)upFreashAction:(MJRefreshNormalHeader*)sender{
    if ([[YPJudgeNet shareJudgeNet] isConnection]) {
        [[YPNetWorking shareNetWorking] getDictWithURL:[YPMakeURL shareMakeURL].homePageHeaderURL dict:[YPMakeURL shareMakeURL].homePageParameterWheelDic success:^(NSDictionary *dic) {
            self.wheelArray = @[].mutableCopy;
            for (NSDictionary *wheelDic in dic[@"data"][@"carousel"]) {
                YPSelectWheelModel *model = [[YPSelectWheelModel alloc]init];
                [model setValuesForKeysWithDictionary:wheelDic];
                [self.wheelArray addObject:model];
            }
            NSMutableArray *arr = @[].mutableCopy;
            for (YPSelectWheelModel *img in self.wheelArray) {
                [arr addObject:img.img];
            }
            SDCycleScrollView *sd = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,110,self.view.width, 140) imageURLStringsGroup:arr];
            sd.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
            sd.delegate = self;
            self.selectVC.tableHeaderView = sd;
        } fail:^(NSError *err) {
        }];
        [[YPNetWorking shareNetWorking] getDictWithURL:[YPMakeURL shareMakeURL].homePageHeaderURL dict:[YPMakeURL shareMakeURL].homePageParameterSelectDic success:^(NSDictionary *dic) {
            _listDataArr = @[].mutableCopy;
            for (NSDictionary *dataDic in dic[@"data"][@"list"]) {
                YPSelectListModel *model = [[YPSelectListModel alloc]init];
                model.imglist = @[].mutableCopy;
                [model setValuesForKeysWithDictionary:dataDic];
                
                if ([model.name isEqualToString:@"音乐"]) {
                    model.coverimg = @"http://pkimg.image.alimmdn.com/upload/20151216/d1999d7e4d7f81b28f2bc3a961c98580.png";
                }
                //是否有用户登陆
                NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"login"];
                if (login == nil || [login isEqualToString:@"nlogin"]) {//初次安装程序，或者已经退出登陆
                    //nothing
                }else{
                    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
                    [self.like isLikeWith:model.listId name:name success:^(BOOL flag) {
                        if (flag) {
                            model.like++;
                        }
                        model.flag = flag;
                    }];
                }
                model.userinfo = [[YPUser alloc]init];
                [model.userinfo setValuesForKeysWithDictionary:dataDic[@"userinfo"]];
                [self.listDataArr addObject:model];
            }
            [self.selectVC reloadData];
            [self.selectVC.mj_header endRefreshing];
        } fail:^(NSError *err) {
            NSLog(@"%@",err);
        }];
    }else{
        [self alertController:@"网络异常"];
        [self.selectVC.mj_header endRefreshing];
    }
}
- (void)getData{
    [[YPNetWorking shareNetWorking] getDictWithURL:[YPMakeURL shareMakeURL].homePageHeaderURL dict:[YPMakeURL shareMakeURL].homePageParameterWheelDic success:^(NSDictionary *dic) {
        self.wheelArray = @[].mutableCopy;
        for (NSDictionary *wheelDic in dic[@"data"][@"carousel"]) {
            YPSelectWheelModel *model = [[YPSelectWheelModel alloc]init];
            [model setValuesForKeysWithDictionary:wheelDic];
            [self.wheelArray addObject:model];
        }
        NSMutableArray *arr = @[].mutableCopy;
        for (YPSelectWheelModel *img in self.wheelArray) {
            [arr addObject:img.img];
        }
        SDCycleScrollView *sd = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,110,self.view.width, 140) imageURLStringsGroup:arr];
        sd.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        sd.delegate = self;
        self.selectVC.tableHeaderView = sd;
    } fail:^(NSError *err) {
    }];
    [[YPNetWorking shareNetWorking] getDictWithURL:[YPMakeURL shareMakeURL].homePageHeaderURL dict:[YPMakeURL shareMakeURL].homePageParameterSelectDic success:^(NSDictionary *dic) {
        [self.dataase deleteSelectSuccess:^{
        } fail:^(NSError *err) {
            NSLog(@"%@",err);
        }];
        
        [self.dataase insertDataWithSelectDic:dic success:^{
        } fail:^(NSError *err) {
            NSLog(@"%@",err);
        }];
        _listDataArr = @[].mutableCopy;
        for (NSDictionary *dataDic in dic[@"data"][@"list"]) {
            YPSelectListModel *model = [[YPSelectListModel alloc]init];
            model.imglist = @[].mutableCopy;
            [model setValuesForKeysWithDictionary:dataDic];
            
            if ([model.name isEqualToString:@"音乐"]) {
                model.coverimg = @"http://pkimg.image.alimmdn.com/upload/20151216/d1999d7e4d7f81b28f2bc3a961c98580.png";
            }
            //是否有用户登陆
            NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"login"];
            if (login == nil || [login isEqualToString:@"nlogin"]) {//初次安装程序，或者已经退出登陆
                //nothing
            }else{
                NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
                [self.like isLikeWith:model.listId name:name success:^(BOOL flag) {
                    if (flag) {
                      model.like++;
                    }
                    model.flag = flag;
                }];
            }
            model.userinfo = [[YPUser alloc]init];
            [model.userinfo setValuesForKeysWithDictionary:dataDic[@"userinfo"]];
            [self.listDataArr addObject:model];
        }
        [self.selectVC reloadData];
        [self.selectVC.mj_header endRefreshing];
    } fail:^(NSError *err) {
        NSLog(@"%@",err);
    }];
}

-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    YPSelectWheelModel *model = _wheelArray[index];
    NSString *webString = @"article/";
    if ([model.url rangeOfString:webString].length > 0) {
        NSArray *array = [model.url componentsSeparatedByString:webString]; //从字符A中分隔成2个元素的数组
        NSString *string = array[1];
        YPWebViewVC *webViewVC = [[YPWebViewVC alloc] init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:webViewVC];
        NSString *url = [NSString stringWithFormat:@"http://pianke.me/webview/%@",string];
        webViewVC.tingID = url;
        webViewVC.type = @"其他";
        [self.parentViewController.navigationController presentViewController:nav animated:YES completion:nil];
    }else {
        [self alertController:@"连接更新中"];
    }
}
- (void)setButtonView{
    ButtonWithview *leftButton = [[ButtonWithview alloc]initWithFrame:CGRectMake(0, 64, self.view.width / 3.0, 44)];
    [leftButton setTitle:@"精选" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    leftButton.isClick = YES;
    leftButton.tag = 1001;
    [self.view addSubview:leftButton];
    
    self.littleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 64 + leftButton.hight, leftButton.width, 2)];
    self.littleLable.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_littleLable];
    ButtonWithview *centerButton = [[ButtonWithview alloc]initWithFrame:CGRectMake(self.view.width / 3.0, 64, self.view.width / 3.0, 44)];
    [centerButton setTitle:@"动态" forState:UIControlStateNormal];
    [centerButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    centerButton.tag = 1002;
    [self.view addSubview:centerButton];
    
    ButtonWithview *rightButton = [[ButtonWithview alloc]initWithFrame:CGRectMake(self.view.width / 3.0*2, 64, self.view.width / 3.0, 44)];
    [rightButton setTitle:@"碎片" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    rightButton.tag = 1003;
    [self.view addSubview:rightButton];
}
- (void)leftButtonAction:(ButtonWithview*)sender{
    [sender setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.littleLable.x = sender.x;
    if (sender.tag == 1001) {
        [self setButtonColorWithButton:sender tag:1002 tag:1003];
        [[YPHomePageDynamicVC shareDynamicVC] removeFromParentViewController];
        [[YPHomePageDynamicVC shareDynamicVC].view removeFromSuperview];
        [[YPHomePageFragmentVC shareFramentVC] removeFromParentViewController];
        [[YPHomePageFragmentVC shareFramentVC].view removeFromSuperview];
    }else if(sender.tag == 1002){
        [self setButtonColorWithButton:sender tag:1001 tag:1003];
        YPHomePageDynamicVC *dynamicVC = [YPHomePageDynamicVC shareDynamicVC];
        dynamicVC.view.y = 108;
        dynamicVC.view.hight = self.view.hight - 108;
        self.view.backgroundColor = [UIColor whiteColor];
        [self addChildViewController:dynamicVC];
        [self.view addSubview:dynamicVC.view];
        [self.view bringSubviewToFront:self.littleLable];
        [[YPHomePageFragmentVC shareFramentVC] removeFromParentViewController];
        [[YPHomePageFragmentVC shareFramentVC].view removeFromSuperview];
    }else{
        [self setButtonColorWithButton:sender tag:1001 tag:1002];
        YPHomePageFragmentVC *fragmentVC = [YPHomePageFragmentVC shareFramentVC];
        self.view.backgroundColor = [UIColor whiteColor];
        fragmentVC.view.y = 108;
        fragmentVC.view.hight = self.view.hight - 108;
        [self addChildViewController:fragmentVC];
        [self.view addSubview:fragmentVC.view];
        [self.view bringSubviewToFront:self.littleLable];
        [[YPHomePageDynamicVC shareDynamicVC] removeFromParentViewController];
        [[YPHomePageDynamicVC shareDynamicVC].view removeFromSuperview];
    }
}

- (void)setButtonColorWithButton:(ButtonWithview*)sender tag:(NSInteger)tag1 tag:(NSInteger)tag2{
    if (!sender.isClick) {//当是NO的时候
        sender.isClick = YES;
    }else{
        ButtonWithview *otherButton1 = [self.view viewWithTag:tag1];
        ButtonWithview *otherButton2 = [self.view viewWithTag:tag2];
        [otherButton1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [otherButton2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
}
#pragma mark ----tablevie delegate 
- (void)downFreash{
    if ([[YPJudgeNet shareJudgeNet] isConnection]) {
        flag += 10;
        [[YPNetWorking shareNetWorking] getDictWithURL:[YPMakeURL shareMakeURL].homePageHeaderURL dict:[[YPMakeURL shareMakeURL] makeDwonFreashDicWithNum:flag] success:^(NSDictionary *dic) {
            for (NSDictionary *dataDic in dic[@"data"][@"list"]) {
                YPSelectListModel *model = [[YPSelectListModel alloc]init];
                model.imglist = @[].mutableCopy;
                [model setValuesForKeysWithDictionary:dataDic];
                if ([model.name isEqualToString:@"音乐"]) {
                    model.coverimg = @"http://pkimg.image.alimmdn.com/upload/20151216/d1999d7e4d7f81b28f2bc3a961c98580.png";
                }
                //是否有用户登陆
                NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"login"];
                if (login == nil || [login isEqualToString:@"nlogin"]) {//初次安装程序，或者已经退出登陆
                    //nothing
                }else{
                    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
                    [self.like isLikeWith:model.listId name:name success:^(BOOL flag) {
                        if (flag) {
                            model.like++;
                        }
                        model.flag = flag;
                    }];
                }
                model.userinfo = [[YPUser alloc]init];
                [model.userinfo setValuesForKeysWithDictionary:dataDic[@"userinfo"]];
                [self.listDataArr addObject:model];
            }
            [self.selectVC reloadData];
            [self.selectVC.mj_footer endRefreshing];
        } fail:^(NSError *err) {
            NSLog(@"%@",err);
        }];
    }else {
        [self alertController:@"无网络连接,无法刷新"];
        [self.selectVC.mj_footer endRefreshing];
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listDataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YPSelectListModel *model = self.listDataArr[indexPath.row];
    if ([model.name isEqualToString:@"插画"] || [model.name isEqualToString:@"摄影"]) {
        YPHomePagePhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YPHomePagePhotoCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.topicLable.text = [NSString stringWithFormat:@"%@.%@",model.name,model.enname] ;
        cell.titleLable.text = model.title;
        cell.detailLable.text = model.content;
        self.worldH = [self stringHight:cell.detailLable.text width:cell.detailLable.width];
        cell.detailLable.width = self.worldH;
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:model.imglist[0][@"imgurl"]]];
        [cell.firstImage sd_setImageWithURL:[NSURL URLWithString:model.imglist[1][@"imgurl"]]];
        [cell.secondImage sd_setImageWithURL:[NSURL URLWithString:model.imglist[2][@"imgurl"]]];
        [cell.likeButton addTarget:self action:@selector(likeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        cell.likeCount.text = [NSString stringWithFormat:@"%ld",(long)model.like];
        cell.likeButton.tag = indexPath.row + 100;
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
        }
        return cell;
    }else if ([model.name isEqualToString:@"碎片"]){
        YPHomePageFragmentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YPHomePageFragmentCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.topicLable.text = model.name;
        cell.detailLable.text = model.content;
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
        NSArray *arr = [model.coverimg_wh componentsSeparatedByString:@"*"];
        CGFloat h = [arr[0] integerValue];
        CGFloat w = [arr[1] integerValue];
        self.imageH = w/h * cell.headerImage.width;
        cell.headerImage.hight = self.imageH;
        self.worldH = [self stringHight:model.content width:cell.detailLable.width];
        cell.detailLable.hight = self.worldH;
        [cell.likeButton addTarget:self action:@selector(likeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        cell.likeCount.text = [NSString stringWithFormat:@"%ld",(long)model.like];
        cell.likeButton.tag = indexPath.row + 100;
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
        }
        return cell;
    }else if ([model.name isEqualToString:@"音乐"]){
        YPHomePageMusicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YPHomePageMusicCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.topicLable.text = [NSString stringWithFormat:@"%@.%@",model.name,model.enname] ;
        cell.titleLable.text = model.title;
        cell.detailLable.text = model.content;
        cell.nameLable.text = model.userinfo.uname;
        [cell.singImage sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
        self.worldH = [self stringHight:model.content width:cell.detailLable.width];
        cell.detailLable.hight = self.worldH;
        [cell.likeButton addTarget:self action:@selector(likeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        cell.likeCount.text = [NSString stringWithFormat:@"%ld",(long)model.like];
        cell.likeButton.tag = indexPath.row + 100;
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
        }
        return cell;
    }else if([model.name isEqualToString:@"Ting"]){
        YPHomePageSoundCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YPHomePageSoundCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.topicLable.text = [NSString stringWithFormat:@"%@.%@",model.name,model.enname] ;
        cell.detailLable.text = model.content;
        [cell.soundImage sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
        self.worldH = [self stringHight:model.content width:cell.detailLable.width];
        cell.detailLable.hight = self.worldH;
        [cell.likeButton addTarget:self action:@selector(likeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        cell.likeCount.text = [NSString stringWithFormat:@"%ld",(long)model.like];
        cell.likeButton.tag = indexPath.row + 100;
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
        }
        return cell;
    }else if ([model.name isEqualToString:@"话题"]){
        YPHomePagePoemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YPHomePagePoemCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.topicLable.text = [NSString stringWithFormat:@"%@.%@",model.name,model.enname] ;
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
         cell.titleLable.text = model.title;
        cell.detailLable.text = model.content;
        self.worldH = [self stringHight:model.content width:cell.detailLable.width];
        [cell.likeButton addTarget:self action:@selector(likeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        cell.likeCount.text = [NSString stringWithFormat:@"%ld",(long)model.like];
        cell.likeButton.tag = indexPath.row + 100;
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
        }
        return cell;
    }else{
        YPHomePageSellectWorldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YPHomePageSellectWorldCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.topicLable.text = [NSString stringWithFormat:@"%@.%@",model.name,model.enname] ;
        [cell.oneImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
        cell.titleLable.text = model.title;
        cell.detailLable.text = model.content;
        self.worldH = [self stringHight:model.content width:cell.detailLable.width];
        cell.detailLable.hight = self.worldH;
        [cell.likeButton addTarget:self action:@selector(likeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        cell.likeCountLable.text = [NSString stringWithFormat:@"%ld",(long)model.like];
        cell.likeButton.tag = indexPath.row + 100;
        [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YPSelectListModel *model = self.listDataArr[indexPath.row];
    if ([model.name isEqualToString:@"Ting"]) {
        YPSongController *songVC = [YPSongController shareSongController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:songVC];
        songVC.tingId = model.listId;
        RadioDetailListModel *radio = [[RadioDetailListModel alloc]init];
        radio.coverimg = model.coverimg;
        radio.authorinfo = model.userinfo;
        radio.title = model.title;
        radio.tingid = model.listId;
        NSMutableArray *array = @[radio].mutableCopy;
        songVC.tempListArr = array;
        songVC.index = 0;
        [self.navigationController presentViewController:nav animated:YES completion:nil];
    }else{
        if ([[YPJudgeNet shareJudgeNet] isConnection]) {
            YPWebViewVC *webVC = [[YPWebViewVC alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:webVC];
            NSString *str = [NSString stringWithFormat:@"http://pianke.me/webview/%@",model.listId];
            webVC.tingID = str;
            webVC.type = model.name;
            webVC.user = model.userinfo;
            webVC.like = model.like;
            webVC.shareTitle = model.title;
            webVC.shareImage = model.coverimg;
            [self.navigationController presentViewController:nav animated:YES completion:nil];
        }else{
            [self alertController:@"请检查网络"];
        }
    }
}
- (void)alertController:(NSString*)str{
   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)likeButtonAction:(UIButton*)sender{
     YPSelectListModel *model = self.listDataArr[sender.tag - 100];
    NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"login"];
    NSString *name  = nil;
    if (login == nil || [login isEqualToString:@"nlogin"]) {
        [self alertController:@"请先登录用户"];
        return;
    }else{
        name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    }
    NSIndexPath *index = [NSIndexPath indexPathForRow:sender.tag - 100 inSection:0];
    model.flag = !model.flag;
    if ([model.name isEqualToString:@"插画"] || [model.name isEqualToString:@"摄影"]) {
        YPHomePagePhotoCell *cell = [self.selectVC cellForRowAtIndexPath:index];
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
             cell.likeCount.text = [NSString stringWithFormat:@"%ld",++model.like];
            [self alertController:@"谢谢喜欢"];
            [self.like addLikeWithUrl:model.listId name:name];
        }else{
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",--model.like];
             [self alertController:@"取消喜欢"];
            [self.like deleteLike:model.listId name:name];
        }
    }else if ([model.name isEqualToString:@"碎片"]){
        YPHomePageFragmentCell *cell = [self.selectVC cellForRowAtIndexPath:index];;
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",++model.like];
              [self alertController:@"谢谢喜欢"];
             [self.like addLikeWithUrl:model.listId name:name];
        }else{
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",--model.like];
             [self alertController:@"取消喜欢"];
            [self.like deleteLike:model.listId name:name];
        }

    }else if ([model.name isEqualToString:@"音乐"]){
        YPHomePageMusicCell *cell = [self.selectVC cellForRowAtIndexPath:index];;
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",++model.like];
              [self alertController:@"谢谢喜欢"];
             [self.like addLikeWithUrl:model.listId name:name];
        }else{
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",--model.like];
             [self alertController:@"取消喜欢"];
            [self.like deleteLike:model.listId name:name];
        }

    }else if([model.name isEqualToString:@"Ting"]){
        YPHomePageSoundCell *cell = [self.selectVC cellForRowAtIndexPath:index];;
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",++model.like];
              [self alertController:@"谢谢喜欢"];
            [self.like addLikeWithUrl:model.listId name:name];
        }else{
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",--model.like];
             [self alertController:@"取消喜欢"];
            [self.like deleteLike:model.listId name:name];
        }
    }else if ([model.name isEqualToString:@"话题"]){
        YPHomePagePoemCell *cell = [self.selectVC cellForRowAtIndexPath:index];;
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",++model.like];
              [self alertController:@"谢谢喜欢"];
             [self.like addLikeWithUrl:model.listId name:name];
        }else{
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
            cell.likeCount.text = [NSString stringWithFormat:@"%ld",--model.like];
             [self alertController:@"取消喜欢"];
            [self.like deleteLike:model.listId name:name];
        }
    }else{
        YPHomePageSellectWorldCell *cell = [self.selectVC cellForRowAtIndexPath:index];
        if (model.flag) {
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:(UIControlStateNormal)];
            cell.likeCountLable.text = [NSString stringWithFormat:@"%ld",++model.like];
              [self alertController:@"谢谢喜欢"];
             [self.like addLikeWithUrl:model.listId name:name];
        }else{
            [cell.likeButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
            cell.likeCountLable.text = [NSString stringWithFormat:@"%ld",--model.like];
            [self alertController:@"取消喜欢"];
            [self.like deleteLike:model.listId name:name];
        }
    }
}

-(CGFloat)stringHight:(NSString*)str width:(CGFloat)width{
    CGRect temp = [str boundingRectWithSize:CGSizeMake(width, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    return temp.size.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    YPSelectListModel *model = self.listDataArr[indexPath.row];
    if ([model.name isEqualToString:@"碎片"]) {
        return self.imageH + self.worldH + 120;
    }else if([model.name isEqualToString:@"音乐"]){
       return 200 + self.worldH;;
    }else if ([model.name isEqualToString:@"Ting"]){
        return 200 + self.worldH;;
    }else if([model.name isEqualToString:@"话题"]){
        return 280 + self.worldH;;
    }else if ([model.name isEqualToString:@"插画"]||[model.name isEqualToString:@"摄影"]){
        return 470 + self.worldH;
    }else{
        return self.worldH + 280;
    }
}
- (YPLikeHelper *)like{
    if (_like == nil) {
        _like = [[YPLikeHelper alloc]init];
    }
    return _like;
}

- (YPDataBase *)dataase{
    if (_dataase == nil) {
        _dataase = [[YPDataBase alloc]init];
    }
    return _dataase;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
